﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace Dictionary.DataLayer
{
    public class FileDir
    {
        public static string dirPath=System.Windows.Forms.Application.StartupPath + "\\Libs";
        public static string dirImage = System.Windows.Forms.Application.StartupPath + "\\Pics";

        //Kiem tra xem co co day du cau truc thu muc hay khong, neu chua thi tao
        public static void CheckDir()
        {
            try
            {
                if (Directory.Exists(dirPath) == false)
                {
                    Directory.CreateDirectory(dirPath);   
                }
                if (Directory.Exists(dirImage) == false)
                {
                    Directory.CreateDirectory(dirImage);
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }

        // Ham lay ten cua tat ca cac file xml trong thu muc libs
        public static ArrayList GetLibs()
        {
            try
            {
                ArrayList arrLib = new ArrayList();
                DirectoryInfo dir = new DirectoryInfo(dirPath);
                foreach (FileInfo file in dir.GetFiles("*.xml"))
                {
                    arrLib.Add(file.Name as string);
                }
                return arrLib;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return null;
            }
        }

        // Ham kiem tra xem file co ton tai hay khong
        public static bool CheckFile(string fn)
        {
            return File.Exists(fn);
        }

        // Ham xoa file
        public static bool DeleteFile(string fn)
        {
            try
            {
                File.Delete(fn);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // Ham xoa Folder
        public static bool DeleteFolder(string fn)
        {
            try
            {
                Directory.Delete(fn,true);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //Ham copy file xml vao thu muc libs
        public static bool CopyToLib(string fn)
        {
            try
            {
                File.Copy(fn, dirPath + "\\" + Path.GetFileName(fn));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // Ham copy hinh anh vao thu muc pics
        public static bool CopyToImage(string folder, string fn, string word)
        {
            try
            {
                File.Copy(fn, dirImage + "\\" + folder + "\\" + word + Path.GetExtension(fn));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //Ham tao folder
        public static void CreateFolder(string fn)
        {
            try
            {
                if (Directory.Exists(fn) == false)
                {
                    Directory.CreateDirectory(fn);
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
    }
}
